//
//  LCZSaveController.m
//  PersonalNews
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZSaveController.h"
#import "LCZFMDatabase.h"
#import "LCZLastColumnModel.h"

@interface LCZSaveController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)LCZBaseLabel *saveLabel;
@property (nonatomic, strong)LCZBaseView *saveView;
@property (nonatomic, strong)LCZBaseTableView *saveTableView;

@property (nonatomic, strong)NSMutableArray *saveArr;
@end

@implementation LCZSaveController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.saveArr = [NSMutableArray array];
    
    // 上面的收藏栏
    self.saveView = [[LCZBaseView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    [self.view addSubview:self.saveView];
    self.saveView.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    
    self.saveLabel = [[LCZBaseLabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 150) / 2, 20, 150, 40)];
    [self.view addSubview:self.saveLabel];
    self.saveLabel.text = @"我的收藏";
    self.saveLabel.textAlignment = NSTextAlignmentCenter;
    
    // 最上面的返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 25, 30, 30);
    backButton.layer.cornerRadius = 15;
    
    backButton.backgroundColor = [UIColor colorWithRed:0.45 green:0.46 blue:0.46 alpha:1];
    [self.view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor whiteColor]];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    

    self.saveTableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStylePlain];
    [self.view addSubview:self.saveTableView];
    self.saveTableView.delegate = self;
    self.saveTableView.dataSource = self;
    self.saveTableView.rowHeight = 50;
    
    [self createData];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.saveArr = [[LCZFMDatabase shareFMDatabase]queryData:2];
    [self.saveTableView reloadData];
    
}


// 返回按钮
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createData
{
    self.saveArr = [[LCZFMDatabase shareFMDatabase]queryData:LCZStyleOfReadModel];
    [self.saveTableView reloadData];
    NSLog(@"%@", self.saveArr);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.saveArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = [self.saveArr[indexPath.row] title];
    return cell;
}

// 左划删除
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        LCZLastColumnModel *lastModel = [[LCZLastColumnModel alloc]init];
        lastModel = self.saveArr[indexPath.row];
        [[LCZFMDatabase shareFMDatabase]clearAll:lastModel];
        [self.saveArr removeObjectAtIndex:indexPath.row];
        [self.saveTableView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
